package xyz.zhangyi.onjava.java8.example.lambda.orchard;

import java.util.ArrayList;
import java.util.List;

public class CustomizedPredicateOrchard extends Orchard {
    @Override
    public List<Apple> filter() {
        return filter(new ApplePredicate() {
            @Override
            public boolean matches(Apple apple) {
                return apple.getColor().equals("green");
            }
        });
    }

    private List<Apple> filter(ApplePredicate predicate) {
        ArrayList<Apple> result = new ArrayList<>();
        for (Apple apple : apples) {
            if (predicate.matches(apple)) {
                result.add(apple);
            }
        }
        return result;
    }

    interface ApplePredicate {
        boolean matches(Apple apple);
    }
}
